package com.bootdo.rate.serivce.impl;

import com.bootdo.payee.dao.PayeeDao;
import com.bootdo.payee.domain.PayeeDO;
import com.bootdo.rate.dao.RateDao;
import com.bootdo.rate.domain.RateDO;
import com.bootdo.rate.serivce.RateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Desc
 * @date 2019/12/23
 *
 */
@Service
public class RateServiceImpl implements RateService {

    @Autowired
    private RateDao rateMapper;
    @Autowired
    private PayeeDao payeeMapper;

    @Override
    public RateDO edit(Long userId){
        RateDO rate = rateMapper.getRate(userId, 0);
        if (rate == null) {
            rate = new RateDO();
        }
        return rate;
    }

    @Override
    public RateDO findPayeeAgentRate(Long userId){
        PayeeDO payee = payeeMapper.get(userId);
        RateDO agnetRate;
        if (payee == null){
            agnetRate = new RateDO();
            agnetRate.setAlipayFixedRate(100.00);
            agnetRate.setWeixinFixedRate(100.00);
            agnetRate.setAlipayTransferRate(100.00);
        } else {
            agnetRate = rateMapper.getRate(payee.getUserIdCreate(), 0);
        }
        return agnetRate;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public int save(RateDO rate){
        if (rate.getId() == null){
            return rateMapper.saveRate(rate);
        } else {
            return rateMapper.updateRate(rate);
        }
    }
}
